<?php

namespace app\web\controller;
use app\web\model\AgentModel;
use app\web\model\OrderprtModel;
use app\web\model\LocationAddressModel;
use app\common\controller\StoreBaseController;
use think\Db;

/**
 * 站点信息
 */
class AgentController extends StoreBaseController
{
 	protected $agent_model;
	protected $orderprt_model;
	protected $location_address;
	
    protected function _initialize() {
        parent::_initialize();
        $this->agent_model = new AgentModel();
		$this->orderprt_model = new OrderprtModel();
		$this->location_address = new LocationAddressModel();
    }
    /**
     * 站点信息
     * @author [longx] 
     */
    public function index(){
    	if(request()->isAjax()){
            $param = input('post.'); 
			
			$result1 = $this->agent_model->allowField(true)->save($param,$param['id']);
			
			//$param['agent_id'] = $this->agent_model->id;
			
			//$result2 = $this->$location_address->allowField(true)->save($param,$param['agent_id']);
			
			//收集店铺地址信息
			$location_address  = new LocationAddressModel();
			$location_address->agent_id  		=	$param['id'];
			$location_address->agent_name  		=	$param['store_name'];
			$location_address->thumb  			=	$param['store_logo'];
			$location_address->business_scope  	=	$param['business_scope'];
			$location_address->address  		=	$param['address'];
			$location_address->longitude  		=	$param['longitude'];
			$location_address->latitude  		=	$param['latitude'];
			$location_address->add_time  		=	now_time();
			
			$my_location = Db::name("mall_location")->where('agent_id',$param['id'])->find();
			
			if($my_location){
				$result2 = $location_address->allowField(true)->save($param,$my_location['id']);
			}else{
				$result2 = $location_address->allowField(true)->save();
			}
			
         	if ($result1 !== false && $result2 !== false) {
                    $this->success('修改成功', url('agent/index'));
            } else {
                $this->error('修改失败');
            }
        }
		$shareUrl = "http://www.auth.cn/web/agent/index.html";
		$agent['shareUrl'] = 'http://www.vshop88.net/code/code.php?url='.urlencode($shareUrl).'&margin=1&size=10';
		$param['id'] = session('storeInfo')['id'];
        $this->assign('ad',$this->agent_model->find($param['id']));
		$this->assign('code_url',$agent['shareUrl']);
		$this->assign('url',$shareUrl);
        return $this->fetch();
    }
	
	 /**
     * 证件信息
     * @author [longx] 
     */
    public function card(){
    	if(request()->isAjax()){
            $param = input('post.');  					       
         	if ($this->agent_model->allowField(true)->save($param,$param['id']) !== false) {
                    $this->success('修改成功', url('agent/card'));
            } else {
                $this->error('修改失败');
            }
        }
		$param['id'] = session('storeInfo')['id'];
        $this->assign('ad',$this->agent_model->find($param['id']));
        return $this->fetch();
    }
	
	/**
     * 其他信息
     * @author [longx] 
     */
    public function other(){
    	if(request()->isAjax()){
            $param = input('post.');  			       
         	if ($this->agent_model->allowField(true)->save($param,$param['id']) !== false) {
                    $this->success('修改成功', url('agent/other'));
            } else {
                $this->error('修改失败');
            }
        }
		$param['id'] = session('storeInfo')['id'];
        $this->assign('ad',$this->agent_model->find($param['id']));
        return $this->fetch();
    }
	
	/**
     * 店铺介绍信息
     * @author [longx] 
     */
    public function detail(){
    	if(request()->isAjax()){
            $param = input('post.');  		       
         	if ($this->agent_model->allowField(true)->save($param,$param['id']) !== false) {
                    $this->success('修改成功', url('agent/detail'));
            } else {
                $this->error('修改失败');
            }
        }
		$param['id'] = session('storeInfo')['id'];
        $this->assign('ad',$this->agent_model->find($param['id']));
        return $this->fetch();
    }
	
	/**
     * [edit 打印机设置]
     * @return [type] [description]
     * @author [longx]
     */
    public function prtedit()
    {
    	$store_id = session('storeInfo')['id'];
    	$orderprt = db('mall_orderprt')->where('store_id',$store_id)->find();
		if($orderprt){
			if(request()->isAjax()){
	            $param = input('post.');		    
	         	if ($this->orderprt_model->allowField(true)->save($param,['id' => $orderprt['id']]) !== false) {
	                    $this->success('保存成功', url('agent/prtedit'));
	            } else {
	                $this->error('保存失败');
	            }
	        }
		}else{
			if(request()->isAjax()){
	        	$param = input('post.');
				$store_id = session('storeInfo')['id'];
				$param['store_id'] = $store_id;
				if ($this->orderprt_model->allowField(true)->save($param) !== false) {
	                    $this->success('保存成功', url('agent/prtedit'));
	            } else {
	                $this->error('保存失败');
	            }
	        }
		}

        $this->assign('ad',$this->orderprt_model->where('store_id',$store_id)->find());
        return $this->fetch();
    }
    /**
	 * 商家财务页面
	 */
	 public function charge(){
	 	$store_id = session('storeInfo')['id'];
        $list = db("mall_score_charge")->where('agent_id',$store_id)->order("add_time desc")->paginate(20);
        return $this->fetch('charge', ['list' => $list]);
    }	
	 
    
    public function beit(){
    	if(input('thumb')){
            $data['thumb']=input('thumb');     
         	if (Db::name('mall_agent')->where('id',input('id'))->update($data) !== false) {
                    $this->success('修改成功', url('agent/beit'));
            } else {
                $this->error('修改失败');
            }
        }
  	$store_id = session('storeInfo')['id'];
  	$ad=$this->agent_model->find($store_id);
    return $this->fetch('beit',['ad'=>$ad]);

    }
}